package com.xsy.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Date;

/**
 * 项目名:    xinguan-parent
 * 类名称:      MyBatisPlusConfig
 * 创建人:     xsy
 * 类描述:
 * 创建时间:    2021/8/26 16:31
 * Version:    1.0
 */
@Configuration
public class MyBatisPlusConfig  implements MetaObjectHandler {
    /**
     * 增加的时候填充走这个方法
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        //填充创建时间
        this.strictInsertFill(metaObject, "createTime", Date.class, new Date());
        //填充修改时间
        this.strictInsertFill(metaObject, "updateTime", Date.class, new Date());
    }

    /**
     * 修改的时候填充走这个方法
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictUpdateFill(metaObject, "updateTime", Date.class, new Date());
    }
    // 最新版(分页插件)
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2));
        return interceptor;
    }
}
